package com.newenergy.wisehome.data.ormlite.dao;

import android.content.Context;

import com.j256.ormlite.dao.Dao;
import com.newenergy.wisehome.common.helper.LogHelper;
import com.newenergy.wisehome.data.bean.ElectricHeater;
import com.newenergy.wisehome.data.ormlite.db.DatabaseHelper;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * 
 * 电暖气数据库操作
 */

public class ElectricHeaterDao {
    private Context mContext;
    private Dao<ElectricHeater, Integer> electricHeaterDaoOpe;
    private DatabaseHelper helper;

    public ElectricHeaterDao(Context context) {
        mContext = context;
        try {
            helper = DatabaseHelper.getHelper(mContext);
            electricHeaterDaoOpe = helper.getDao(ElectricHeater.class);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    /**
     * 增加一个用户
     *
     * @param
     * @throws SQLException
     */
    public void add(ElectricHeater electricHeater) {
        try {
            electricHeater.setChecked(false);
            electricHeaterDaoOpe.create(electricHeater);
        } catch (SQLException e) {
            e.printStackTrace();
        }

    }


    public int updateElectricHeater(ElectricHeater electricHeater) {
        try {
            electricHeater.setChecked(false);
            return electricHeaterDaoOpe.update(electricHeater);
            //    LogHelper.i("updateLightElectricHeaternumber" + number);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return -1;
    }

    public ElectricHeater get(int id) {
        try {
            return electricHeaterDaoOpe.queryForId(id);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    public ElectricHeater getByUuid(String uuid) {
        List<ElectricHeater> electricHeaterList = null;
        try {
            electricHeaterList = electricHeaterDaoOpe.queryBuilder().where().eq
                    ("uuid", uuid).query();
            if (electricHeaterList != null && electricHeaterList.size() > 0) {
                return electricHeaterList.get(0);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * @return
     */
    public List<ElectricHeater> getAllElectricHeater() {
        List<ElectricHeater> electricHeaterList = new ArrayList<>();
        try {
            electricHeaterList = electricHeaterDaoOpe.queryBuilder().query();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return electricHeaterList;
    }

    public ElectricHeater getById(String groupId,int category) {
        List<ElectricHeater> electricHeaterList = null;
        try {
            electricHeaterList = electricHeaterDaoOpe.queryBuilder().where().eq
                    ("groupId", groupId).and().eq
                    ("category", category).query();
            if (electricHeaterList != null && electricHeaterList.size() > 0) {
                return electricHeaterList.get(0);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }


    public List<ElectricHeater> getFamilyElectricHeaters(String familyId,int category) {
        List<ElectricHeater> electricHeaterList = new ArrayList<>();
        try {
            electricHeaterList = electricHeaterDaoOpe.queryBuilder().where().eq ("familyId", familyId).and().eq
                    ("category", category).query();

        } catch (SQLException e) {
            e.printStackTrace();
        }
        return electricHeaterList;
    }

    public int delete(ElectricHeater electricHeater) {
        try {
            return electricHeaterDaoOpe.delete(electricHeater);
            //    LogHelper.i("deleLightElectricHeaternumber==" + number);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return -1;
    }

    public void deleteAll() {
        try {
            int number = electricHeaterDaoOpe.deleteBuilder().delete();
            // LogHelper.i("deleteAll number==" + number);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public void delete(List<ElectricHeater> electricHeaterList) {
        try {
            int number = electricHeaterDaoOpe.delete(electricHeaterList);
            LogHelper.i("delete number==" + number);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }


    public void deleteFamilyElectricHeaters(String familyId) {
        try {
            List<ElectricHeater> electricHeaterList = getFamilyElectricHeaters(familyId,4);
            int number = electricHeaterDaoOpe.delete(electricHeaterList);
            LogHelper.i("delete number==" + number);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public ElectricHeater getElectricHeaterByName(String familyId, String name,int category) {
        try {
            List<ElectricHeater> electricHeaterList = electricHeaterDaoOpe.queryBuilder().where().eq
                    ("familyId", familyId).and().eq
                    ("name", name).and().eq
                    ("category", category).query();
            if (electricHeaterList != null && electricHeaterList.size() > 0)
                return electricHeaterList.get(0);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
}
